/*
 * ProductPanel.java
 *
 * Created on Feb 25, 2010, 12:32:48 PM
 */

package dat215projekt.gui.components;

import dat215projekt.data.IMatDataHandlerExtended;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import se.chalmers.cse.dat215.project.Product;

/**
 *
 * @author ludwigk
 */
public class ProductPanel extends javax.swing.JPanel {
    private static final IMatDataHandlerExtended handler = IMatDataHandlerExtended.getInstance();

    private Product product;

    private static final Color rowStripeColor_1 = new Color(240,245,250);
    private static final Color rowStripeColor_2 = new Color(255,255,255);

    public ProductPanel(){}


    /** Creates new form ProductPanel */
    public ProductPanel(Product p, int num) {
        initComponents();

        // Set product information
        this.product = p;
        nameLabel.setText(p.getName());
        priceLabel.setText(p.getPrice()+" "+p.getUnit());
        amountLabel.setText("Antal ("+p.getUnitSuffix()+"):");

        // 180, 135
        imageLabel.setIcon(IMatDataHandlerExtended.getInstance().getImageIcon(p,180,135));

        //imageLabel.setIcon(IMatDataHandlerExtended.getInstance().getImageIcon(p));

        // Set favorite button state
        favoriteButton.setSelected(handler.isFavorite(p));

        // Set favorite help text
        if(!favoriteButton.isSelected())
            favoriteHelpText.setText("<html><center>Lägg till favorit</center></html>");
        else
            favoriteHelpText.setText("<html><center>Ta bort favorit</center></html>");

        // Add product button action listener
        addProductButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                 // Check if the input is valid
                try{
                    int amount = Integer.parseInt(amountTextField.getText());
                    handler.addToShoppingCart(product, amount);
                }catch(NumberFormatException e){
                    // If not a a valid number, set back to 1
                    amountTextField.setText("1");
                }
            }
        });

        favoriteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(favoriteButton.isSelected()) // Add as favorite
                    handler.addFavorite(product);
                else // Remove from favorites
                    handler.removeFavorite(product);
            }
        });

         // Set background color for row-striping
        if(num % 2 == 1){
            this.setBackground(rowStripeColor_1);
        }
        else{
             this.setBackground(rowStripeColor_2);
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        imageLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        priceLabel = new javax.swing.JLabel();
        amountLabel = new javax.swing.JLabel();
        amountTextField = new javax.swing.JTextField();
        addProductButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        favoriteHelpText = new javax.swing.JLabel();
        favoriteButton = new javax.swing.JToggleButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dat215projekt.Dat215projektApp.class).getContext().getResourceMap(ProductPanel.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N

        imageLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        imageLabel.setName("imageLabel"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setMaximumSize(new java.awt.Dimension(400, 135));
        jPanel1.setMinimumSize(new java.awt.Dimension(135, 135));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setOpaque(false);

        nameLabel.setFont(resourceMap.getFont("priceLabel.font")); // NOI18N
        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("priceLabel.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        priceLabel.setFont(resourceMap.getFont("priceLabel.font")); // NOI18N
        priceLabel.setText(resourceMap.getString("priceLabel.text")); // NOI18N
        priceLabel.setName("priceLabel"); // NOI18N

        amountLabel.setFont(resourceMap.getFont("amountTextField.font")); // NOI18N
        amountLabel.setName("amountLabel"); // NOI18N

        amountTextField.setFont(resourceMap.getFont("amountTextField.font")); // NOI18N
        amountTextField.setText(resourceMap.getString("amountTextField.text")); // NOI18N
        amountTextField.setName("amountTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(dat215projekt.Dat215projektApp.class).getContext().getActionMap(ProductPanel.class, this);
        addProductButton.setAction(actionMap.get("addSome")); // NOI18N
        addProductButton.setFont(resourceMap.getFont("amountTextField.font")); // NOI18N
        addProductButton.setText(resourceMap.getString("addProductButton.text")); // NOI18N
        addProductButton.setToolTipText(resourceMap.getString("addProductButton.toolTipText")); // NOI18N
        addProductButton.setName("addProductButton"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(nameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 239, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(priceLabel))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(amountLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(amountTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(addProductButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(priceLabel)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addProductButton)
                    .addComponent(amountLabel)
                    .addComponent(amountTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setOpaque(false);
        jPanel2.setLayout(new java.awt.GridBagLayout());

        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setOpaque(false);
        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.LINE_AXIS));
        jPanel2.add(jPanel3, new java.awt.GridBagConstraints());

        favoriteHelpText.setFont(resourceMap.getFont("favoriteHelpText.font")); // NOI18N
        favoriteHelpText.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        favoriteHelpText.setText(resourceMap.getString("favoriteHelpText.text")); // NOI18N
        favoriteHelpText.setName("favoriteHelpText"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        jPanel2.add(favoriteHelpText, gridBagConstraints);

        favoriteButton.setIcon(resourceMap.getIcon("favoriteButton.icon")); // NOI18N
        favoriteButton.setText(resourceMap.getString("favoriteButton.text")); // NOI18N
        favoriteButton.setContentAreaFilled(false);
        favoriteButton.setName("favoriteButton"); // NOI18N
        favoriteButton.setSelectedIcon(resourceMap.getIcon("favoriteButton.selectedIcon")); // NOI18N
        favoriteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                favoriteButtonActionPerformed(evt);
            }
        });
        jPanel2.add(favoriteButton, new java.awt.GridBagConstraints());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void favoriteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_favoriteButtonActionPerformed
        if(favoriteButton.isSelected()){
            handler.addFavorite(product);
            favoriteHelpText.setText("<html><center>Ta bort favorit</center></html>");

        }
        else{
            handler.removeFavorite(product);
            favoriteHelpText.setText("<html><center>Lägg till favorit</center></html>");
        }

    }//GEN-LAST:event_favoriteButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addProductButton;
    private javax.swing.JLabel amountLabel;
    private javax.swing.JTextField amountTextField;
    private javax.swing.JToggleButton favoriteButton;
    private javax.swing.JLabel favoriteHelpText;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel priceLabel;
    // End of variables declaration//GEN-END:variables

}
